package com.hsteach.web.controller.calendar;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.poi.ExcelUtil;
import com.hsteach.dao.domain.calendar.Calendar;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.service.calendar.CalendarService;
import com.hsteach.web.controller.calendar.dto.AddCalendarDto;
import com.hsteach.web.controller.calendar.dto.AddHolidayDto;
import com.hsteach.web.controller.calendar.dto.UpdateCalendarDto;
import com.hsteach.web.controller.calendar.dto.UpdateDayDto;
import com.hsteach.web.dto.PageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 周行事历主Controller
 * 
 * @author fanxquer
 * @date 2023-11-28
 */
@Api(value = "周行事历管理", tags = "周行事历管理")
@RestController
@RequestMapping("/web/calendar")
public class CalendarController extends BaseController
{
    @Autowired
    private CalendarService calendarService;

    /**
     * 查询周行事历主列表
     */
    @ApiOperation("分页查询周行事历")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PageDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Calendar query = new Calendar();
        query.setDelStatus(BooleanEnum.TRUE.getCode());
        query.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
        List<Calendar> list = calendarService.selectCalendarList(query);
        List<UpdateCalendarDto> ret = list.stream().map(e -> {
            UpdateCalendarDto vo = new UpdateCalendarDto();
            BeanUtils.copyProperties(e, vo);
            return vo;
        }).collect(Collectors.toList());
        TableDataInfo dataTable = getDataTable(ret);
        long total = new PageInfo(list).getTotal();
        dataTable.setTotal(total);
        return dataTable;
    }



    /**
     * 查询周行事历详情
     */
    @ApiOperation("查询周行事历详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable("id") Long id)
    {
        return success(calendarService.selectCalendarById(id));
    }

    /**
     * 获取周行事历主详细信息
     */
    @ApiOperation("查询周行事历日历明细")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id)
    {
        return success(calendarService.detail(id));
    }

    /**
     * 导出周行事历日历明细
     */
    @ApiOperation("导出周行事历日历明细")
    @GetMapping(value = "/detail/export/{id}")
    public AjaxResult exportDetail(@PathVariable("id") Long id)
    {
        String export = calendarService.export(id);
        AjaxResult success = AjaxResult.success();
        Map<String, String> data = Maps.newHashMap();
        data.put("url", export);
        success.put("data", data);
        return success;
    }


    @ApiOperation("获取节假日列表")
    @GetMapping(value = "/holidaySet/{calendarId}")
    public AjaxResult holidaySet(@PathVariable("calendarId") Long calendarId)
    {
        return success(calendarService.getHolidays(calendarId));
    }

    /**
     * 新增周行事历主
     */
    @ApiOperation("新增周行事历")
    @PostMapping
    public AjaxResult add(@RequestBody AddCalendarDto dto)
    {
        return toAjax(calendarService.add(dto));
    }

    /**
     * 修改周行事历主
     */
    @ApiOperation("修改周行事历")
    @PutMapping
    public AjaxResult edit(@RequestBody UpdateCalendarDto dto)
    {
        Calendar update = new Calendar();
        BeanUtils.copyProperties(dto, update);
        update.setUpdateBy(SecurityUtils.getAuthInfo().getPersonId());
        return toAjax(calendarService.updateCalendar(update));
    }

    /**
     * 修改单日放假情况
     */
    @ApiOperation("修改单日放假情况")
    @PutMapping("/detail/day")
    public AjaxResult editDay(@RequestBody UpdateDayDto dto)
    {
        return toAjax(calendarService.editDay(dto));
    }

    /**
     * 增加节假日
     */
    @ApiOperation("增加节假日")
    @PostMapping("/detail/holiday")
    public AjaxResult addHoliday(@RequestBody AddHolidayDto dto)
    {
        return toAjax(calendarService.addHoliday(dto));
    }

    /**
     * 删除周行事历主
     */
    @ApiOperation("删除周行事历")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        Calendar update = new Calendar();
        update.setId(id);
        update.setDelStatus(BooleanEnum.FALSE.getCode());
        return toAjax(calendarService.updateCalendar(update));
    }
}
